package org.jeecg.modules.wmhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.constant.UserConstant;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.wmhb.entity.*;
import org.jeecg.modules.wmhb.mapper.WmPurchaseTaskbookBomMapper;
import org.jeecg.modules.wmhb.service.IWmPurchaseTaskbookBomService;
import org.jeecg.modules.wmhb.vo.WmApplyBomVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 采购任务书BOM
 * @Author: jeecg-boot
 * @Date:   2024-08-31
 * @Version: V1.0
 */
@Service
public class WmPurchaseTaskbookBomServiceImpl extends ServiceImpl<WmPurchaseTaskbookBomMapper, WmPurchaseTaskbookBom> implements IWmPurchaseTaskbookBomService {
    @Override
    public void importWmPurchaseTaskbookBom(PurchaseBom purchaseBom, WmTask wmTask){
        WmPurchaseTaskbookBom wmPurchaseTaskbookBom = new WmPurchaseTaskbookBom();
        LambdaQueryWrapper<WmPurchaseTaskbookBom> queryWrapperTaskExist = new LambdaQueryWrapper<>();
        queryWrapperTaskExist.eq(WmPurchaseTaskbookBom::getName, purchaseBom.getName()).eq(WmPurchaseTaskbookBom::getWmTaskId, wmTask.getId()).last("LIMIT 1");
        WmPurchaseTaskbookBom wmPurchaseTaskbookBomExist = getOne(queryWrapperTaskExist);
        if(wmPurchaseTaskbookBomExist == null) {
            String seriesNo = (String) FillRuleUtil.executeRule(FillRuleConstant.BOM_ORDER, null);
            wmPurchaseTaskbookBom.setWmTaskbookId(wmTask.getWmTaskbookId());
            wmPurchaseTaskbookBom.setCode(purchaseBom.getCode());
            wmPurchaseTaskbookBom.setSeriesNo(seriesNo);
            wmPurchaseTaskbookBom.setWmTaskId(wmTask.getId());
            wmPurchaseTaskbookBom.setName(purchaseBom.getName());
            wmPurchaseTaskbookBom.setQuantity(purchaseBom.getQuantity());
            wmPurchaseTaskbookBom.setSpecification(purchaseBom.getSpecification());
            wmPurchaseTaskbookBom.setUnitOfMeasure(purchaseBom.getUnitOfMeasure());
            wmPurchaseTaskbookBom.setItemOrProduct(purchaseBom.getItemOrProduct());
            wmPurchaseTaskbookBom.setIsContainElectric(purchaseBom.getIsContainElectric());
            wmPurchaseTaskbookBom.setIsContainControl(purchaseBom.getIsContainControl());
            wmPurchaseTaskbookBom.setIsContainMainMaterial(purchaseBom.getIsContainMainMaterial());
            wmPurchaseTaskbookBom.setIsContainAssistMaterial(purchaseBom.getIsContainAssistMaterial());
            wmPurchaseTaskbookBom.setIsContainInstall(purchaseBom.getIsContainInstall());
            wmPurchaseTaskbookBom.setIsContainDebug(purchaseBom.getIsContainDebug());
            save(wmPurchaseTaskbookBom);
        }
    }

    @Override
    public void updateWmPurchaseTaskbookBom(PurchaseBom purchaseBom, WmTask wmTask){
        LambdaUpdateWrapper<WmPurchaseTaskbookBom> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WmPurchaseTaskbookBom::getCode, purchaseBom.getCode())
                .eq(WmPurchaseTaskbookBom::getName, wmTask.getName())
                .eq(WmPurchaseTaskbookBom::getWmTaskId, wmTask.getId());
        update(null, updateWrapper);
    };

    @Override
    public void purchaseByTask(String wmTaskId){
        LambdaUpdateWrapper<WmPurchaseTaskbookBom> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WmPurchaseTaskbookBom::getStatus, UserConstant.PURCHASE_TASKBOOK_STATUS_PURCHASED)
                .set(WmPurchaseTaskbookBom::getPurchaseTime, new Date())
                .eq(WmPurchaseTaskbookBom::getWmTaskId, wmTaskId);
        update(null, updateWrapper);
    };

    @Override
    public void arriveByTask(String wmTaskId){
        LambdaUpdateWrapper<WmPurchaseTaskbookBom> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WmPurchaseTaskbookBom::getStatus, UserConstant.PURCHASE_TASKBOOK_STATUS_ARRIVED)
                .set(WmPurchaseTaskbookBom::getArriveTime, new Date())
                .eq(WmPurchaseTaskbookBom::getWmTaskId, wmTaskId);
        update(null, updateWrapper);
    };

    @Override
    public List<WmPurchaseTaskbookBom> listByWmTaskId(String wmTaskId){
        LambdaQueryWrapper<WmPurchaseTaskbookBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmPurchaseTaskbookBom::getWmTaskId, wmTaskId);
        List<WmPurchaseTaskbookBom> wmPurchaseTaskbookBomList = list(queryWrapper);
        return wmPurchaseTaskbookBomList;
    };

    @Override
    public List<WmPurchaseTaskbookBom> listByWmTaskbookId(String wmTaskbookId){
        LambdaQueryWrapper<WmPurchaseTaskbookBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmPurchaseTaskbookBom::getWmTaskbookId, wmTaskbookId);
        List<WmPurchaseTaskbookBom> wmPurchaseTaskbookBomList = list(queryWrapper);
        return wmPurchaseTaskbookBomList;
    };

    @Override
    public List<WmPurchaseTaskbookBom> listByWmTaskIdAndStartEnd(String wmTaskId, String firstDayOfMonth, String lastDayOfMonth){
        LambdaQueryWrapper<WmPurchaseTaskbookBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmPurchaseTaskbookBom::getWmTaskId, wmTaskId).ge(WmPurchaseTaskbookBom::getSendTime, firstDayOfMonth).le(WmPurchaseTaskbookBom::getSendTime, lastDayOfMonth);
        List<WmPurchaseTaskbookBom> wmPurchaseTaskbookBomList = list(queryWrapper);
        return wmPurchaseTaskbookBomList;
    };

    @Override
    public ModelAndView exportBomExcel(HttpServletRequest request, List<WmPurchaseTaskbookBom> wmPurchaseTaskbookBomList){
        //1、、遍历数据，添加多个 sheet 的数据及其对应的实体类
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<PurchaseBom> sheetList = new ArrayList<PurchaseBom> ();
        for(WmPurchaseTaskbookBom wmPurchaseTaskbookBom: wmPurchaseTaskbookBomList){
            PurchaseBom purchaseBom = new PurchaseBom();
            purchaseBom.setName(wmPurchaseTaskbookBom.getName());
            purchaseBom.setCode(wmPurchaseTaskbookBom.getCode());
            purchaseBom.setQuantity(wmPurchaseTaskbookBom.getQuantity());
            purchaseBom.setUnitOfMeasure(wmPurchaseTaskbookBom.getUnitOfMeasure());
            purchaseBom.setSpecification(wmPurchaseTaskbookBom.getSpecification());
            purchaseBom.setItemOrProduct(wmPurchaseTaskbookBom.getItemOrProduct());
            purchaseBom.setIsContainDebug(wmPurchaseTaskbookBom.getIsContainDebug());
            purchaseBom.setIsContainControl(wmPurchaseTaskbookBom.getIsContainControl());
            purchaseBom.setIsContainInstall(wmPurchaseTaskbookBom.getIsContainInstall());
            purchaseBom.setIsContainElectric(wmPurchaseTaskbookBom.getIsContainElectric());
            purchaseBom.setIsContainMainMaterial(wmPurchaseTaskbookBom.getIsContainMainMaterial());
            purchaseBom.setIsContainAssistMaterial(wmPurchaseTaskbookBom.getIsContainAssistMaterial());
            sheetList.add(purchaseBom);
        }

        Map<String, Object> map = new HashMap<>();
        map.put(NormalExcelConstants.CLASS, PurchaseBom.class);
        map.put(NormalExcelConstants.PARAMS, new ExportParams(null, "采购物料清单", ExcelType.XSSF));
        map.put(NormalExcelConstants.DATA_LIST, sheetList);
        dataList.add(map);

        SimpleDateFormat format = new SimpleDateFormat("MMddHHmm");
        String fileName = "BOM_".concat(format.format(new Date())); //名称为中文报错
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, fileName);
        mv.addObject(NormalExcelConstants.MAP_LIST, dataList);
        return mv;
    };

    @Override
    public List<WmPurchaseTaskbookBom> isBomExist(PurchaseBom purchaseBom, WmTask wmTask){
        LambdaQueryWrapper<WmPurchaseTaskbookBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmPurchaseTaskbookBom::getWmTaskId, wmTask.getId()).eq(WmPurchaseTaskbookBom::getName, purchaseBom.getName());
        List<WmPurchaseTaskbookBom> wmPurchaseTaskbookBomList = list(queryWrapper);
        return wmPurchaseTaskbookBomList;
    };

    @Override
    public List<WmPurchaseTaskbookBom> queryNotArriveBom(){
        LambdaQueryWrapper<WmPurchaseTaskbookBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(WmPurchaseTaskbookBom::getArriveTime);
        List<WmPurchaseTaskbookBom> wmPurchaseTaskbookBomList = list(queryWrapper);
        return wmPurchaseTaskbookBomList;
    };
}
